/**
*  \file input.h
*
*  \brief
*
*  \author Michael J. Hohmann  michael.hohmann@linde-mh.de
*
*
*
*
*
*/
#ifndef INPUT_H_INCLUDED
#define INPUT_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"
#include "../trilean/trilean.h"
#include "../direction_switch/direction_switch.h"
#include "../mcu/mcu.h"

/*================================================[ public defines   ]================================================*/
#define div_1000  0.001F
/*================================================[ public datatypes ]================================================*/

enum accelerator_pedal_type
{
    INVALID_PEDAL,
    SINGLE_PEDAL,
    DUAL_PEDAL,
    RESERVE_PEDAL,
};

enum steer_angle_sensor_type
{
    INVALID_ANGLE_SENSOR,
    SINGLE_CHANNEL_ANGLE_SENSOR,
    DUAL_CHANNEL_ANGLE_SENSOR,
    RESERVE_ANGLE_SENSOR,
};

enum steer_direction
{
    steer_natrual = 0,
    steer_right = 1,
    steer_left = 0,
};
struct steer_data
{
    S16 angle;
    enum steer_direction direct;
};

enum lift_option_type
{
    lift_single_switch = 1,
    lift_dual_switch = 2,
    lift_potentiometer = 3
};
/** Object definition.
* 
* The struct input_obj defines the attribute of an object.
*
*    if( state_time <= 0)
      {
         rv = sdec_s16( state_time); 
      }
      else
      {
         rv = -1;
      }
   
*/
struct input_data_obj
{
    F32 adc_voltage[mcu_adc_max_chn];
    struct steer_data steer;

    S16 accelerator;                                  /**<   [0.01 Percent]    */
    S16 accelerator_valid;                            /**<                     */

    enum accelerator_pedal_type pedal_type;           /**< default  1 single pedal , 2 dual pedal  */
    enum steer_angle_sensor_type steer_sensor_type;   /**< default  1 single channel , 2 dual channel  */

    S16 accelerator_neutral;
    S16 temperature_motor_2;                         /**<  (PT1000, NTC ready)X1,16: pump_temperature */
    S16 temperature_motor_1;                         /**<  (PT1000, NTC ready)X1,7:  traction_temperature*/
    S16 temperature_motor_controller;                /**<  from can         */

    enum direction_switch_state direction;           /**<   X1,22: Forward   X1,23 reverse   X1,30 neutral */
    enum direction_switch_error direction_error;     /**<   direction error     */

    S16 main_contactor_error;
    S16 iso_valve_error;
    S16 buzzer_error;
    S16 fan_error;
    S16 boot_anchor_cpu_m;

    /* pump */
    S16 lift_switch_1;                              /**<   X1,14:          */
    S16 lift_switch_2;                              /**<   X1,19:		   */
    S16 tilt_switch;                                /**<   X1,15:          */
    S16 hyd_aux_switch_1;                           /**<   X1,18:          */
    S16 hyd_aux_switch_2;                           /**<   from CAN        */
    S16 lift_potentiometer_ratio;                   /**<   [0.01 Percent]  */
    enum lift_option_type lift_option;

    S16 key_switch;
    S16 reserve_input;

    S16 seat_switch_1;                              /**<   ticks how long the seat switch signal was TRUE  */
    S16 seat_switch_2;                              /**<   might come via can                              */
    S16 park_break_switch;                          /**<   X1,25:                                          */

    S16 battery;

    S16 truck_speed;                                /**<   from CAN                        */
    S16 motor_speed;                                /**<   trac rpm  from speed sensor     */
    BO torque_enable;
    //BO in_bHBrake_N;                              /**<   hand brake  state               */

    F32 pump_sensor_speed_A;                        /**<   X1,24   From M can */
    F32 pump_sensor_speed_B;                        /**<   X1,33   From M can */

    F32 traction_sensor_speed_A;                    /**<   X1,31    */
    F32 traction_sensor_speed_B;                    /**<   X1,32    */

    U16 v_debug_rx_0[4];


    

    enum trilean driver_present;/**<  */

    // flags for creep limp  home etc */
    //S16 battery_voltage;
    S16 direct_link_voltage;

    F32 mL_iMot_Can;
    F32 mL_fqSta_Can;
    F32 mL_TMot_Can;                        /**< trac_motorl_temp   */
    F32 mL_TMos_Can;                        /**< trac_chip_temp   */
    BO mL_StsError_Can;
    BO mL_StsReady_Can;
    BO mL_StsPwmOn_Can;
    BO mL_StsMagnetized_Can;
    BO mL_StsHwEnabled_Can;
    BO mL_StsSwEnabled_Can;
    /* .. = b_T_ConvLocked_Lft_b   ; */
    /* .. = b_T_NoPwmSet_Lft_b     ; */
    S16 mL_HBeatCnt_Can;                   /**< trac_motorl_active_cnt */

    F32 mL_mEval_Can;                      /**< trac_motorl_*/
    F32 mL_nSen_Can;                       /**< trac_motorl_speed      */
    F32 mL_iBat_Can;
    F32 mL_xSta_Can;
    U16 mL_DTC_Can[4];
    BO  mL_vt2_dm4;
    BO  mL_vt2_dm3;
    BO  mL_vt2_dm2;
    BO  mL_vt2_dm1;
  
    U16 mE_DTC_Can[4]; 
    F32 mE_mEval_Can;                      /** < pump motor torque  */
    F32 mE_nSen_Can;                       /** < pump motor speed   */
    F32 mE_iBat_Can;                       /** < current  battery   */
    /* .. = b_L1_statorCurrent_b   ; */
    F32 mE_TMot_Can;                       /** < pump motor temperature  */
    F32 mE_TMos_Can;                       /** < pump motor chip temperature  */

    /* .. = b_L1_statorFreq_b      ; */
    BO mE_StsError_Can;                    /** < pump motor no error  */
    BO mE_StsReady_Can; 
    BO mE_StsPwmOn_Can;
    BO mE_StsMagnetized_Can;
    BO mE_StsHwEnabled_Can;
    BO mE_StsSwEnabled_Can;
    S16 mE_statorCurrent_can;
    S16 mE_statorFreq_can;
    BO mE_VL2_dm4;
    BO mE_VL2_dm3;
    BO mE_VL2_dm2;
    BO mE_VL2_dm1;

    
    /* .. =  b_L1_ConvLocked_b     ; */
    /* .. =  b_L1_NoPwmSet_b       ; */
    S16 mE_HBeatCnt_Can;
    U32 ba_DateTime_Can;
    S16 te_Angle;



	/*
    S16 m_steer_angle_can;           // can_int
    U8 m_park_break_in_d_can;        // can_int
    U8 m_neutral_position_in_d_can;  
    U8 m_reverse_in_d_can;
    U8 m_forward_in_d_can;
    U8 m_inv2_enable_can;
    U8 m_inv1_enable_can;
    U8 m_seat_switch_in_d_can;
    U8 m_seat_switch_nc_in_d_can;
    S16 m_accelerator_can;
    S16 m_dc_link_in_a_can;
    U8 m_power_board_variant_can;
    S16 m_5v_current_sensor_in_a_can;
    S16 m_137v_supply_in_a_can;
    U8 m_key_switch_in_d_can;
    */
	
    S32 bms_battery_current;   /** < bms battery current, -3000-3000, unit A, factor 0.001  */
    U32 bms_battery_volts;     /** < bms battery voltage, 0-130, unit V, factor 0.001  */
    U8 bms_battery_state;
    U8 bms_soh;                /** < units %  */
    U16 bms_max_charge_current2;  /** < factor 0.25, units A  */
    U16 bms_max_discharge_current2;  /** < factor 0.25, units A  */
    U16 bms_max_charge_current100;  /** < factor 0.25, units A  */
    U16 bms_max_discharge_current100;  /** < factor 0.25, units A  */
    U16 bms_dtc1;
    U16 bms_dtc2;
    U16 bms_dtc3;
    U16 bms_dtc4;
    U32 bms_min_discharge_volts;/** < factor 0.001, units v  */
    U32 bms_max_charge_volts;/** < factor 0.001, units v  */
    U8  bms_soc_without_soh; /** < units %  */
    U8  bms_soc_with_soh;    /** < units %  */
    U32 bms_output_volts;/** < factor 0.001, units v  */
    U32 bms_max_discharge_power2;/** < factor 10, units w  */
    U32 bms_max_charge_power2;/** < factor 10, units w  */
    U8  bms_max_battery_temp;/** < factor 1, offset -40  */
    U8  bms_min_battery_temp;/** < factor 1, offset -40  */
    U16 bms_max_cell_volts;/** < factor 0.001, units v  */
    U16 bms_min_cell_volts;/** < factor 0.001, units v  */
    U8 bms_state;

};

/*================================================[ public variables ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
/**
*  Initialization.
*
*  Initialize the input module.
*
*  \return RC_SUCCESS if successful.
*/
RC input_initialize( void );

/**
*/
void input_read( struct input_data_obj *p_data );
S16  analog_calculate_ratio(F32 input_accelerator_voltage, F32 min, F32 max);
S16 analog_calculate_ratio_2(F32 steer_A_vol, F32 natrual, F32 right_vol, F32 left_vol, struct steer_data *steer);
F32 in_Time           ( VO );
/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2019 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
